package algorithm.leetcode.I201to400;

import java.util.Stack;

public class Q232 {
}


class MyQueue {

    Stack<Integer> mainStack;
    Stack<Integer> minorStack;

    /** Initialize your data structure here. */
    public MyQueue() {
        mainStack = new Stack<>();
        minorStack = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        mainStack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        // main留一个,其他全部倒腾到minor里,公共动作1
        leaveOneInMain();
        // 取出main剩下的这一个
        int pop = mainStack.pop();
        // minor全部再倒腾回main,,公共动作2
        minor2Main();
        return pop;
    }

    /** Get the front element. */
    public int peek() {
        // main留一个,其他全部倒腾到minor里,公共动作1
        leaveOneInMain();
        // 取出main剩下的这一个
        int peek = mainStack.pop();
        // 这一个再push到minor里
        minorStack.push(peek);
        // minor全部再倒腾回main,公共动作2
        minor2Main();
        return peek;
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return mainStack.isEmpty() && minorStack.isEmpty();
    }

    private void leaveOneInMain() {
        while (mainStack.size() > 1) {
            minorStack.push(mainStack.pop());
        }
    }

    private void minor2Main() {
        while (!minorStack.isEmpty()) {
            mainStack.push(minorStack.pop());
        }
    }

}